package com.huike.controller;

import com.huike.core.controller.BaseController;
import com.huike.core.domain.AjaxResult;
import com.huike.core.domain.R;
import com.huike.dto.BusinessDto;
import com.huike.dto.BusinessRecordDto;
import com.huike.dto.ClueFalseDto;
import com.huike.dto.GainClueDto;
import com.huike.dto.domain.TbBusiness;
import com.huike.pojo.OpportunityFollowUp;
import com.huike.result.MyPageResult;
import com.huike.result.PageResult;
import com.huike.service.BusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/business")
@Slf4j
class BusinessController  extends BaseController {
    @Autowired
    private BusinessService businessService;
    /**
     * 分页查询商机
     * @param businessDto
     * @return
     */
    @GetMapping("/list")
    public MyPageResult getBusinessListPage(BusinessDto businessDto ) {
        log.info("分页查询商机，{}",businessDto);
        PageResult businessListPage = businessService.getBusinessListPage(businessDto);
        return MyPageResult.ok(businessListPage.getRecords(),businessListPage.getTotal());
    }
    /**
     * 新增商机
     * @param business
     * @return
     */
    @PostMapping
    public AjaxResult addClue(@RequestBody TbBusiness business ) {
        log.info("新增商机，{}",business);
        businessService.addBusiness(business);
        return success();
    }
    /**
     * 批量分配商机
     * @param gainClueDto
     * @return
     */
    @PutMapping("/assignment")
    public AjaxResult assignmentBusinessByIds(@RequestBody GainClueDto gainClueDto ) {
        log.info("批量分配商机，{}",gainClueDto);
        businessService.assignmentBusinessByIds(gainClueDto);
        return success();
    }
    /**
     * 通过id查看商机
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public AjaxResult getBusinessById(@PathVariable Integer id ) {
        log.info("通过id查看商机，{}",id);
        return success(businessService.getBusinessById(id));
    }
    /**
     * 查询商机跟进记录
     * @param
     * @return
     */
    @GetMapping("/record/list")
    public AjaxResult getBusinessRecordList(@RequestParam Integer businessId) {
        log.info("查询商机跟进记录，{}",businessId);
        List<OpportunityFollowUp> rows = businessService.getBusinessRecordList(businessId);
        return success(rows);
    }

    /**
     * 新增商机跟进记录
     * @param businessRecordDto
     * @return
     */
    @PostMapping("/record")
    public AjaxResult addBusinessRecord(@RequestBody BusinessRecordDto businessRecordDto ) {
        log.info("新增商机跟进记录，{}",businessRecordDto);
        businessService.addBusinessRecord(businessRecordDto);
        return success();
    }
    /**
     * 退回公海
     * @param id
     * @return
     */
    @PutMapping("/back/{id}/{reasons}")
    public AjaxResult backBusinesseById(@PathVariable Integer id ,@PathVariable Integer reasons ) {
        log.info("退回公海，{},{}",id,reasons);
        businessService.backBusinesseById(id,reasons);
        return success();
    }
    /**
     * 批量捞取
     * @param gainClueDto
     * @return
     */
    @PutMapping("/gain")
    public AjaxResult gainBusinessByIds(@RequestBody GainClueDto gainClueDto ) {
        log.info("批量捞取，{}",gainClueDto);
        businessService.gainBusinessByIds(gainClueDto);
        return success();
    }
}
